package com.lanou;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) throws IOException {
        // 爬虫 爬取页面
        // https://search.jd.com/Search?keyword=%E7%AC%94%E8%AE%B0%E6%9C%AC&enc=utf-8

        int count = 0;
        for (int i = 1; i <= 10; i++) {

            String url = "https://search.jd.com/Search?keyword=%E7%AC%94%E8%AE%B0%E6%9C%AC&enc=utf-8&page=i";
            String url1 = "https://search.jd.com/Search?keyword=%E7%A9%BA%E8%B0%83&enc=utf-8&page=i";
            Document document = Jsoup.connect(url1).get();
            // System.out.println(document);
            // 爬取名字为...类选择器
            Elements elements = document.select(".gl-item");
            // System.out.println(elements.size());
            // #J_goodsList > ul > li:nth-child(7)
            for (Element element : elements) {
                // System.out.println(element);
                // 标题 div > div.p-name > a > em
                // html带标签
                // String html = element.select("div > div.p-name > a > em").html();
                // 标题
                String text = element.select("div > div.p-name > a > em").text();
                // 卖点
                String sell_point = element.select("div > div.p-name > a > i").text();
                // 价格
                String price = element.select("div > div.p-price > strong > i").text();
                // 图片
                String img = element.select("div > div.p-img > a > img").attr("source-data-lazy-img");

                //System.out.println("html:" + html);
                System.out.println("标题:" + text);
                System.out.println("卖点:" + sell_point);
                System.out.println("价格:" + price);
                System.out.println("图片:" + img);
                System.out.println("---------------------------------------------");
                count++;
            }
        }
        System.out.println("共爬取" + count + "商品");
    }
}
